package service

import (
	"crypto/tls"
	"fmt"
	"io/ioutil"
	"net/http"
	"rongyy/src/middlewares"
	"rongyy/src/models"
)

type ReportService struct {
}

func (pc *ReportService) Report(req *models.ReqReport) (interface{}, error) {
	data, err := (&HisService{}).GetOpReport(req)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3013 failed ", err)
		return nil, err
	}
	fmt.Println(data.Body.DocumentRetrievalRp)

	if len(data.Body.DocumentRetrievalRp) > 0 {
		if len(data.Body.DocumentRetrievalRp[0].DocumentRetrievalRp) > 0 {
			arr := make([]models.Document, 0)
			if len(data.Body.DocumentRetrievalRp[0].DocumentRetrievalRp[0].Document) > 0 {

				for _, item := range data.Body.DocumentRetrievalRp[0].DocumentRetrievalRp[0].Document {
					if item.DocumentID != "648478" {
						arr = append(arr, item)
					}
				}
			}
			data.Body.DocumentRetrievalRp[0].DocumentRetrievalRp[0].Document = arr
		}
	}
	return data.Body.DocumentRetrievalRp, nil
}
func (pc *ReportService) ReportPdf(req *models.ReqReportPdf) (interface{}, error) {
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr}
	resp, err := client.Get(req.Url)
	if err != nil {
		fmt.Println("error:", err)
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3013 failed ", err)
		return nil, err
	}
	return string(body), nil
}
